<template>
  <div id="app">
    <h1>
      根组件
      - {{ title }}
      - {{ $store.state.count }}
    </h1>
    <input type="text" :value="count" @input="handleInput">
    <Son1></Son1>
    <hr>
    <Son2></Son2>
  </div>
</template>

<script>
// mapState 本质上就是将仓库中的数据转成计算属性的vuex辅助函数
import { mapState } from 'vuex'
import Son1 from './components/Son1.vue'
import Son2 from './components/Son2.vue'
// console.log(mapState(['count', 'title']))

export default {
  name: 'app',
  created () {
    console.log(this.$store.state.count)
  },
  computed: {
    // 展开运算符
    ...mapState(['count', 'title'])
  },
  data: function () {
    return {
      inpValue: ''
    }
  },
  components: {
    Son1,
    Son2
  },
  methods: {
    handleInput (e) {
      clearTimeout(this.timer)
      this.timer = setTimeout(() => {
        // 前面加 + 号可以转成 number
        const number = +e.target.value
        this.$store.commit('changeCount', number)
      }, 0)
    }
  }
}
</script>

<style>
#app {
  width: 600px;
  margin: 20px auto;
  border: 3px solid #ccc;
  border-radius: 3px;
  padding: 10px;
}
</style>
